package cn.saltice.idempotence.idem.config;

import cn.saltice.idempotence.idem.distributed.DistributedLock;
import cn.saltice.idempotence.idem.distributed.DistributedLockRedis;
import cn.saltice.idempotence.idem.enums.IdempotentStorageTypeEnum;
import cn.saltice.idempotence.idem.idempotent.DistributedIdempotent;
import cn.saltice.idempotence.idem.idempotent.IdempotentImpl;
import cn.saltice.idempotence.idem.store.IdempotentStorageFactory;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * @program: idempotence
 * @description:
 * @author: Mr.Liu
 * @create: 2020-10-08 19:50
 **/
@Configuration
@ConditionalOnBean({RedissonAutoConfiguration.class})
public class IdempotentConfig {
    @Autowired
    private IdempotentStorageFactory factory;

    /**
     * 锁，对key加锁用的
     * @param redissonClient
     * @return
     */
    @Bean
    public DistributedLock getDistributedLock(RedissonClient redissonClient){
        return new DistributedLockRedis(redissonClient,null);
    }

    /**
     * 幂等执行
     * @return
     */
    @Bean
    public DistributedIdempotent getDistributedIdempotent(){
        return new IdempotentImpl(factory.getIdempotentStorage(IdempotentStorageTypeEnum.REDIS),factory.getIdempotentStorage(IdempotentStorageTypeEnum.MYSQL));
    }
}
